﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.DataOperate;


namespace BLL.Business
{
    public class ManagerBLL
    {
        DAL.DataOperate.ManagerDAL managerDAL = new ManagerDAL();

        public DataSet SelectAll()
        {
            return managerDAL.SelectAll();
        }
        public DataSet SelectByName(String ManagerName)
        {
            if (ManagerName == "")
                return null;
            else
                return managerDAL.SelectByName(ManagerName);
        }
        public DataSet SelectByID(int ManagerId)
        {
            if (ManagerId.ToString() == "")
                return null;
            else
                return managerDAL.SelectByID(ManagerId);
        }

        /// <summary>
        ///  判断管理员存在否
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns>不存在：false
        ///          存在：  true</returns>
        public bool IsExist(String ManagerName)
        {
            DataSet ds = new DataSet();
            ds = SelectByName(ManagerName);

            if ((ds == null) ||
                (ds.Tables.Count == 0) ||
                (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
                return false;
            }
            else
                return true;
        }

        public bool LoginSucess(String ManagerName, String ManagerPassword)
        {
            DataSet ds = new DataSet();
            String password = "";
            ds = SelectByName(ManagerName);

            if (!IsExist(ManagerName))
            {
                return false;
            }
            else
            {
                if ((ds == null) ||
              (ds.Tables.Count == 0) ||
              (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                    return false;
                }
                else
                {
                    password = ds.Tables[0].Rows[0].ItemArray[1].ToString();
                    if (password == ManagerPassword)
                    {
                        return true;
                    }
                    else
                        return false;
                }
            }
        }

        public bool InsertManager(Model.Manager manager)
        {
            if (manager.Manager_Namestr == "" || manager.Manager_Passwordstr == "")
            {
                return false;
            }
            else
            {
                if (!IsExist(manager.Manager_Namestr))
                {
                    return managerDAL.Insert(manager);
                }
                else
                {
                    return false;
                }
            }
        }

        public bool UpdateManager(Model.Manager manager)
        {
            if (manager.Manager_Namestr == "" || manager.Manager_Passwordstr == "")
            {
                return false;
            }
            else
            {
                return managerDAL.Update(manager);
            }
        }
        public bool DeleteManager(int ManagerId)
        {
            if (ManagerId.ToString() == "")
            {
                return false;
            }
            else
            {
                return managerDAL.Delete(ManagerId);
            }
        }
    }
}
